library(ggplot2)
library(dplyr)

1 Loading data

#BiocManager::install("Hiiragi2013")
library(Hiiragi2013)
library(Biobase)
data(x)
expression <- Biobase::exprs(x)
dftx <- data.frame(pData(x), t(expression))
head(pData(x))
##         File.name Embryonic.day Total.number.of.cells lineage genotype
## 1 E3.25  1_C32_IN         E3.25                    32               WT
## 2 E3.25  2_C32_IN         E3.25                    32               WT
## 3 E3.25  3_C32_IN         E3.25                    32               WT
## 4 E3.25  4_C32_IN         E3.25                    32               WT
## 5 E3.25  5_C32_IN         E3.25                    32               WT
## 6 E3.25  6_C32_IN         E3.25                    32               WT
##           ScanDate sampleGroup sampleColour
## 1 E3.25 2011-03-16       E3.25      #CAB2D6
## 2 E3.25 2011-03-16       E3.25      #CAB2D6
## 3 E3.25 2011-03-16       E3.25      #CAB2D6
## 4 E3.25 2011-03-16       E3.25      #CAB2D6
## 5 E3.25 2011-03-16       E3.25      #CAB2D6
## 6 E3.25 2011-03-16       E3.25      #CAB2D6
dim(expression)
## [1] 45101   101
groupSize <- table(dftx$sampleGroup) 
groupSize
## 
##           E3.25 E3.25 (FGF4-KO)      E3.5 (EPI)  E3.5 (FGF4-KO) 
##              36              17              11               8 
##       E3.5 (PE)      E4.5 (EPI)  E4.5 (FGF4-KO)       E4.5 (PE) 
##              11               4              10               4
ggplot( dftx, aes( x = X1426642_at, y = X1418765_at ))  +
  geom_point( aes( color = sampleGroup), shape = 19 ) +
  geom_smooth( method = "loess" ) +
  scale_color_discrete( guide = FALSE )

2 Plotly

#install.packages("plotly")
library(plotly)
plt <- ggplot( dftx, aes( x = X1426642_at, y = X1418765_at ))  +
  geom_point( aes( color = sampleGroup), shape = 19 ) +
  geom_smooth( method = "loess" ) +
  scale_color_discrete( guide = FALSE )
ggplotly(plt) 
plot_ly(dftx, x = ~X1426642_at, y = ~X1418765_at, z = ~X1416967_at, 
        color = ~Embryonic.day, type = "scatter3d", mode = "markers",
        marker = list(size = 4))

3 Tydying data

selectedProbes <- c(Fgf4  = "X1420085_at", Gata4 =  "X1418863_at",      
                    Gata6 = "X1425463_at",  Sox2 = "X1416967_at") 
genes_expression <- as.matrix(dftx[, selectedProbes])
head(genes_expression)
##         X1420085_at X1418863_at X1425463_at X1416967_at
## 1 E3.25    3.027715    4.843137    5.500618    1.731217
## 2 E3.25    9.293016    5.530016    6.160900    9.697038
## 3 E3.25    2.940142    4.418059    4.584961    4.161240
## 4 E3.25    9.715243    5.982314    4.753439    9.540123
## 5 E3.25    8.924228    4.923580    4.629728    8.705340
## 6 E3.25   11.325952    4.068520    4.165692    8.696228
library("reshape2")
genes = melt(genes_expression, varnames = c("sample", "probe"))
head(genes)
genes$gene =
  names(selectedProbes)[match(genes$probe, selectedProbes)]
head(genes)
##    sample       probe     value gene
## 1 1 E3.25 X1420085_at  3.027715 Fgf4
## 2 2 E3.25 X1420085_at  9.293016 Fgf4
## 3 3 E3.25 X1420085_at  2.940142 Fgf4
## 4 4 E3.25 X1420085_at  9.715243 Fgf4
## 5 5 E3.25 X1420085_at  8.924228 Fgf4
## 6 6 E3.25 X1420085_at 11.325952 Fgf4
wide <- dcast(genes, formula =  sample ~ probe, value.var = "value")
head(wide)
##    sample X1420085_at X1418863_at X1425463_at X1416967_at
## 1 1 E3.25    3.027715    4.843137    5.500618    1.731217
## 2 2 E3.25    9.293016    5.530016    6.160900    9.697038
## 3 3 E3.25    2.940142    4.418059    4.584961    4.161240
## 4 4 E3.25    9.715243    5.982314    4.753439    9.540123
## 5 5 E3.25    8.924228    4.923580    4.629728    8.705340
## 6 6 E3.25   11.325952    4.068520    4.165692    8.696228
head(genes)
##    sample       probe     value gene
## 1 1 E3.25 X1420085_at  3.027715 Fgf4
## 2 2 E3.25 X1420085_at  9.293016 Fgf4
## 3 3 E3.25 X1420085_at  2.940142 Fgf4
## 4 4 E3.25 X1420085_at  9.715243 Fgf4
## 5 5 E3.25 X1420085_at  8.924228 Fgf4
## 6 6 E3.25 X1420085_at 11.325952 Fgf4

4 1D data plots

p = ggplot(genes, aes( x = gene, y = value))
p + geom_boxplot(aes(fill = gene))

p + geom_boxplot() +
  geom_jitter(aes(color = gene), width = 0.1, height = 0)

p = ggplot(genes, aes( x = gene, y = value))
p + geom_violin(aes(fill =  gene))

p + geom_violin() +
  geom_jitter(aes(color = gene), width = 0.1, height = 0)

p = ggplot(genes %>% filter(gene %in% c("Gata4", "Sox2")),
           aes(x = value)) 
p + geom_histogram(aes(fill = gene), 
                   color = "white", bins = 40)

p + geom_histogram(
  aes(fill = gene), color="white", alpha=0.6, 
  bins = 40, position = "identity")

p = ggplot(genes, aes( x = value, color = gene)) 
p + geom_density()

p = ggplot(genes, aes( x = value, fill = gene)) 
p + geom_density(alpha = 0.3)

dfx = as.data.frame(Biobase::exprs(x))
ggplot(dfx, aes(x = `64 E4.5 (EPI)`)) + geom_histogram(bins = 100)

ggplot(dfx, aes(x = exp(`64 E4.5 (EPI)`))) + geom_histogram(binwidth = 20) +
  xlim(0, 1500)
## Warning: Removed 7302 rows containing non-finite values (stat_bin).
## Warning: Removed 2 rows containing missing values (geom_bar).

simdata = rnorm(70)
simdf <- data.frame(index = seq(along = simdata), sx = sort(simdata))
ggplot(simdf, aes(x = sx, y = index)) +  geom_step()

4.1 change theme

g = ggplot(iris, 
           aes(x = Species, 
               y = Sepal.Length, 
               fill = Species))+
  geom_violin(col = NA)
g

g + theme_minimal()

g + theme_dark()

library(ggthemes)
g + theme_economist_white()

5 3-5D data

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
ggplot(data = mtcars) +
  geom_point(
    aes(x = wt, y = mpg,
        shape = factor(gear),
        color = factor(cyl), 
        size = qsec))

6 Title page

# Title page
library(ggplot2)
library(viridis)
library(season)
library(gridExtra)


pd<-ggplot(schz, aes(year, month, fill = SczBroad)) + 
  geom_tile(colour="gray20", size=1.5, stat="identity") + 
  scale_fill_viridis(option="D") +
  scale_y_continuous(breaks=1:12, labels=month.abb[1:12])+
  xlab("") + 
  ylab("") +
  ggtitle("Total Australian Schizophrenics Born By Month and Year") +
  theme(
    plot.title = element_text(color="white",hjust=0,vjust=1, size=rel(2)),
    plot.background = element_rect(fill="gray20"),
    panel.background = element_rect(fill="gray20"),
    panel.border = element_rect(fill=NA,color="gray20", size=0.5, linetype="solid"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.line = element_blank(),
    axis.ticks = element_blank(), 
    axis.text = element_text(color="white", size=rel(1.5)),
    axis.text.y  = element_text(hjust=1),
    legend.text = element_text(color="white", size=rel(1.3)),
    legend.background = element_rect(fill="gray20"),
    legend.position = "bottom",
    legend.title=element_blank()
  )

pd